const TotalCourseModel = require('../db/models/totalCourse');


class TotalCourseServer { 
  async addTotalCourseData (data) {
    //检查cid是否存在 check cid is existed
    const cid = data.cid; 
    
    const result = await TotalCourseModel.findOne({where: { cid }});
    //判断 check
    if (result) { 
      //更新 update  
      return await TotalCourseModel.update(data, {where: { cid }})
    }else {
      //创建 create
      return await TotalCourseModel.create(data);
    }
  }

  
  async getCourseData () {
    const data =  await TotalCourseModel.findAll({
      attributes: {
        exclude: ['posterImgUrl', 'description', 'createdAt', 'updatedAt']
      }  
    });
    return data;
    // return null;
  }

  /* 改变课程分类*/
  async changeField (cid, field) {
    const result = await TotalCourseModel.update({ field }, {
      where: { cid }
    });
    //返回值是存有1或0的数组，取第零项
    return result[0]; 
  }

  async changeCourseStatus (id, status) {
    //公用中间件控制器
    const result =  await TotalCourseModel.update({ status }, {
      where: { cid: id }
    })
    return result[0];
  }

}

module.exports = new TotalCourseServer();